﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daco.Web.UI;
using Daco.Web.Store;

namespace Daco.Web.Pages.EStore
{
    public partial class Catalog :  SitePage
    {
        public Catalog()
        {
            this.Load += new EventHandler(Catalog_Load);
        }

        void Catalog_Load(object sender, EventArgs e)
        {
            ccProductList.CategoryFilters = ccSearchForm.CategoryFilters;

            // Setup page title
            string originTitle = Page.Title;

            Page.Title = "Sản phẩm - " + originTitle;

            if (ccSearchForm.CategoryFilters != null && ccSearchForm.CategoryFilters.Length == 1)
            {
                CProductCategory category = CProductCategory.GetCategory((int)ccSearchForm.CategoryFilters[0]);
                if (category != null)
                {
                    lTitle.Text =  category.GetFullTitle(" &raquo; ");
                    lDescription.Text = category.Description;

                    // Setup page title
                    Page.Title = lTitle.Text + " - " + originTitle;

                    ccProductList.ShowBox = true;
                }
            }

            if (ccSearchForm.ManufactureFilters != null && ccSearchForm.ManufactureFilters.Length == 1)
            {
                CManufacture manufacture = CProduct.GetManufacture((int)ccSearchForm.ManufactureFilters[0]);
                if (manufacture.ManufactureID > 0)
                {
                    lTitle.Text = string.Format("({0}){1}", manufacture.Name, lTitle.Text);
                    lDescription.Text += "<div>" + manufacture.Description + "</div>";

                    // Setup page title
                    Page.Title = lTitle.Text + " - " + originTitle;

                    ccProductList.ShowBox = true;
                }
            }

            ccProductList.ManufactureFilters = ccSearchForm.ManufactureFilters;
            ccProductList.ContainName = ccSearchForm.ContainName;
            ccProductList.PageSize = ccSearchForm.PageSize;
            ccProductList.PriceFrom = ccSearchForm.PriceFrom;
            ccProductList.PriceTo = ccSearchForm.PriceTo;
            ccProductList.SeriesFilters = ccSearchForm.SeriesFilters;

            int pageIndex = string.IsNullOrEmpty(Request.QueryString["p"]) ? 1 : int.TryParse(Request.QueryString["p"], out pageIndex) ? pageIndex : 1;
            ccProductList.PageIndex = pageIndex;
            ccProductList.DisplayPage = ccSearchForm.FormatedSearchPage;
        }
    }
}